// JPQR Chrome Extentions
// A generic onclick callback function.
function genericOnClick(info, tab) {
    var canvas = document.getElementById('qrcode');
    if (!canvas || !canvas.getContext) {
        return false;
    }
    var ctx = canvas.getContext('2d');
    var img = new Image();
//    console.log(info.)
    console.log(info.srcUrl);
    img.src = info.srcUrl;

    img.onload = function(){
        canvas.width = img.width;
        canvas.height = img.height;
        ctx.drawImage(img, 0, 0);
        var imagedata = ctx.getImageData(0, 0, img.width, img.height);
        imagedata.getPoints = function(x, y){
            if (this.width < x) {
                throw new Error("point error");
            }
            if (this.height < y) {
                throw new Error("point error");
            }
            point = (x * 4) + (y * this.width * 4);
            p = new RGBColor(this.data[point], this.data[point + 1], this.data[point + 2], this.data[point + 3]);
            return p;
        }
        var qr = new QRCode();
        var result = qr.getContsnts(imagedata, img.width);
        // CR LF -> br TAG
        result = result.replace("\x0D\x0A", "<br>");
//        document.getElementById("qrcode-content").innerHTML = result;
        console.log(result);
    	var notification = webkitNotifications.createNotification(
    			info.srcUrl,
    			chrome.i18n.getMessage("qrdecode_decoded"),
    			result);
    	notification.show();
        return result;
    }


	console.log("item " + info.menuItemId + " was clicked");
	console.log("info: " + JSON.stringify(info));
	console.log("tab: " + JSON.stringify(tab));
}


// Create one test item for each context type.
	var context = "image";
	var title = chrome.i18n.getMessage("qrdecode_decode_context");
	try{
	var id = chrome.contextMenus.create( {
		"title" : title,
		"contexts" : [ context ],
		"onclick" : genericOnClick
	}
	);
//	console.log(id);
	}catch(e){
		console.log(e);
	}



